import {
    protocol
} from './protocol'
let WS = 'wss://chat.52112.com/wss/'

export class CWebSocket {
    constructor() {
        this.listenerList = {}
        this.waitingList = []
        this.socket = new WebSocket(WS)
        this.socket.onopen = () => {
            this.checkWaitingList()
        }
        this.socket.onmessage = (...args) => {
            this.S2CMessage(...args)
        }
        this.socket.onerror = (...args) => {
            this.onerror(args)
        }
        for (let key in protocol) {
            this[key] = protocol[key]
        }
    }
    checkWaitingList() {
        this.waitingList.forEach(item => {
            let protocol, data = item
            this.C2SMessage(protocol, data)
        })
    }
    addListener(protocol, func) {
        if (!this.listenerList[protocol]) {
            this.listenerList[protocol] = []
        }
        this.listenerList[protocol].push(func)
    }
    removeListener(protocol, func) {
        if (!func) {
            this.listenerList[protocol] = undefined
            return
        }
        if (!this.listenerList[protocol]) return
        let index = this.listenerList.findIndex(func)
        index !== -1 && this.listenerList[protocol].splice(index, 1)
    }
    S2CMessage(event) {
        let msg = JSON.parse(event.data);

        let {
            protocol,
            data
        } = msg

        if (!this.listenerList[protocol]) return

        this.listenerList[protocol].forEach(func => func(data))
    }
    C2SMessage(protocol, data) {
        let msg;
        if (this.socket.readyState !== this.socket.OPEN) {
            this.waitingList.push([protocol, data])
            return
        }
        if(!protocol) {
            msg = {
                protocol: data[0],
                data: data[1]
            };
        } else {
            msg = {
                protocol,
                data
            }
        }
        // console.log(msg)
        this.socket.send(JSON.stringify(msg))
    }
    onerror(error) {
        console.log('error', error)
    }
    clearEvent() {
        this.socket.onopen = null
        this.socket.onmessage = null
        this.socket.onerror = null
    }
    close() {
        if (this.socket) {
            for (let protocol in this.listenerList) {
                this.removeListener(protocol)
            }
            this.clearEvent()
            this.socket.close()
            this.socket = null
        }
    }
}